<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>对象创建的方式</title>
</head>
<body>

</body>
<script type="text/javascript">
   /** Object构造函数模式
        * var p = new Object()
        p = {}
        p.name = 'tom'
        p.age = 13
        p.setName = function (name){
            this.name = name
        }
        p.setAge = function (age){
            this.age = age
        }
        p.setName('JJ')
        console.log(p)
    */

  /** 对象字面量
       * var p = {
           name:'kfc',
           age:14,
           setName:function (name){
               this.name = name
           }
       }
       p.setName('kkk')
       console.log(p.name,p.age)
   */

  /** 工厂模式
       * function createPerson(name,age){
          var p = {
              name:name,
              age:age,
              setAge:function (age){
                  this.age = age
              }
          }
          return p
      }
      var p1 = createPerson('AA',12)

       console.log(p1)
   */

   /** 自定义构造函数模式
        * function Person(name,age){
           this.name = name
           this.age = age
           this.setName = function (name){
               this.name = name
           }
       }
        var p1 = new Person('Abc',12)
        var p2 = new Person('Kfc',11)
        console.log(p1, p1 instanceof Person) // instanceof:检测是否是该构造函数的实例
    */

   /** 构造函数+原型组合模式
        *function Person(name,age){
           this.name = name
           this.age = age

       }
        Person.prototype.setName = function (name){
           this.name = name
       }

        var p1 = new Person('Abc',12)
        var p2 = new Person('Efg',13)
        p1.setName('Abc1')
        Person.prototype.setAge = function (age){
           this.age = age
       }
        p1.setAge(22)


        Person.prototype = {}

        p1.setAge(30)
        console.log(p1)
        var p3 = new Person('BOB',12)
        p3.setAge(10)
        console.log(p3)
    */





</script>
</html>

